/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Panel.java
 *
 * Created on Jan 13, 2011, 10:42:48 PM
 */
package View;

import filetrans.Client;
import filetrans.Control;
import java.io.File;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author CarlosScipione
 */
public class Panel extends javax.swing.JFrame {

    private Client Client;
    private Login Login;
    private static ArrayList threadList= new ArrayList();

    /** Creates new form Panel */
    public Panel(Login login, Client client) {

        initComponents();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        this.Client = client;

        this.Login = login;
        this.setVisible(true);

//        jButton1.setEnabled(false);
//        UploadButton.setEnabled(false);
//        LocalFileList.setEnabled(false);
//        LocalFileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
//        LocalFileList.setDragEnabled(false);

    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        RemoteFileList = new javax.swing.JList();
        DownloadButton = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        LocalFileList = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Transmisor");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jScrollPane1.setViewportView(RemoteFileList);

        DownloadButton.setText("Descargar");
        DownloadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DownloadButtonActionPerformed(evt);
            }
        });

        jButton3.setText("Refrescar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel1.setText("Archivos Remotos:");

        jButton1.setText("Desconectar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(LocalFileList);

        jLabel2.setText("Descargas:");

        jButton2.setText("Detener");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton4.setText("Reanudar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(jLabel2)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 167, Short.MAX_VALUE)
                                .add(DownloadButton))
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                    .add(3, 3, 3)
                                    .add(jLabel1)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 120, Short.MAX_VALUE)
                                    .add(jButton3))
                                .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2)
                                .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 340, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .add(17, 17, 17))
                    .add(layout.createSequentialGroup()
                        .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 87, Short.MAX_VALUE)
                        .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 77, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 70, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel1)
                    .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 38, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 204, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(DownloadButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 204, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 19, Short.MAX_VALUE)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents



    public void cleanUp(){

        File dir = new File(System.getProperty("user.dir") + "/files");
            File[] isFolder = dir.listFiles();
            int count = 0;
            for (int i = 0; i < isFolder.length; i++) {
                if (isFolder[i].isDirectory() || isFolder[i].isHidden()) {
                    count++;
                }
            }

            String[] files = new String[isFolder.length - count];
            count = 0;
            for (int i = 0; i < isFolder.length; i++) {
                if (!isFolder[i].isDirectory() && !isFolder[i].isHidden()) {
                    files[count] = isFolder[i].getName();
                    count++;
                }
            }
        LocalFileList.setListData(files);


    }
    public void checkArray(String name){
        int q = 0;
            for (int i = 0; i < threadList.size(); i++) {
                if (((ArrayList) threadList.get(i)).get(0).equals(name)) {
                    q = i;
                }
            }
        threadList.remove(q);
    }

    public void regenerate(){
             Control control = new Control();
            ArrayList twoPack = new ArrayList();
            twoPack.add((String) RemoteFileList.getSelectedValue());
            twoPack.add(control.setInstance((String) RemoteFileList.getSelectedValue(),this, Client));

            threadList.add(twoPack);
    }

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
 RemoteFileList.setEnabled(true);

        try {
         
            RemoteFileList.setListData(Client.remoteDir());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Error", JOptionPane.ERROR_MESSAGE);
        }



        if (RemoteFileList.getModel().getElementAt(0).equals("Directory Empty")) {
            RemoteFileList.setEnabled(false);
          
        }
    }//GEN-LAST:event_formWindowOpened

    private void validateDownloadList() {

//        String[] files = null;
//        int quantity = 0;
//
//        File dir = new File(System.getProperty("user.dir") + "/files");
//        File[] isFolder = dir.listFiles();
//        int count = 0;
//        for (int i = 0; i < isFolder.length; i++) {
//            if (isFolder[i].isDirectory() || isFolder[i].isHidden()) {
//                count++;
//            }
//        }
//
//        String[] files = new String[isFolder.length - count];
//        count = 0;
//        for (int i = 0; i < isFolder.length; i++) {
//            if (!isFolder[i].isDirectory() && !isFolder[i].isHidden()) {
//                files[count] = isFolder[i].getName();
//                count++;
//            }
//        }
//
//
//
//        String message = null;
//        ObjectOutputStream oos = new ObjectOutputStream(
//                socket.getOutputStream());
//        message += files.length;
//        message += "@end|begin@";
//        if (files != null) {
//            for (int i = 0; i < files.length; i++) {
//                message += files[i];
//                message += "@end|begin@";
//            }
//        } else {
//            message = "Empty";
//        }
//
//        quantity = Integer.parseInt(message.substring(4,
//                message.indexOf("@end|begin@")));
//        message = message.substring(message.indexOf("@end|begin@")
//                + ("@end|begin@").length(), message.length());
//
//        if (quantity != 0) {
//            files = new String[quantity];
//        }
//
//        for (int i = 0; i < quantity; i++) {
//            files[i] = message.substring(0, message.indexOf("@end|begin@"));
//            message = message.substring(message.indexOf("@end|begin@")
//                    + ("@end|begin@").length(), message.length());
//        }
//
    }


    private void DownloadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DownloadButtonActionPerformed
        if (!((String) RemoteFileList.getSelectedValue()).equals("")) {
            Control control = new Control();
            ArrayList twoPack = new ArrayList();
            twoPack.add((String) RemoteFileList.getSelectedValue());
            twoPack.add(control.setInstance((String) RemoteFileList.getSelectedValue(),null, Client));

            threadList.add(twoPack);
        } else {
            JOptionPane.showMessageDialog(null,
                    "Debes seleccionar un archivo",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_DownloadButtonActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
 RemoteFileList.setEnabled(true);

        try {
           Client client = new Client();
            RemoteFileList.setListData(client.remoteDir());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Error", JOptionPane.ERROR_MESSAGE);
        }



        if (RemoteFileList.getModel().getElementAt(0).equals("Directory Empty")) {
            RemoteFileList.setEnabled(false);

        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        //Detengo Todas las descargas
        
        this.setVisible(false);
        this.Login.reset();

    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed

        this.setVisible(false);
        this.Login.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_formWindowClosed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (!((String) LocalFileList.getSelectedValue()).equals("")) {
            int q = 0;
            for (int i = 0; i < threadList.size(); i++) {
                if (((ArrayList) threadList.get(i)).get(0).equals((String) LocalFileList.getSelectedValue())) {
                    ((Thread) ((ArrayList) threadList.get(i)).get(1)).interrupt();
                    q = i;
                }
            }
            threadList.remove(q);
        } else {
            JOptionPane.showMessageDialog(null,
                    "Debes seleccionar un archivo",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        if (!((String) LocalFileList.getSelectedValue()).equals("")) {
            Control control = new Control();
            ArrayList twoPack = new ArrayList();
            twoPack.add((String) LocalFileList.getSelectedValue());
            twoPack.add(control.setInstance((String) LocalFileList.getSelectedValue(), null,Client));

            threadList.add(twoPack);
        } else {
            JOptionPane.showMessageDialog(null,
                    "Debes seleccionar un archivo",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton DownloadButton;
    private javax.swing.JList LocalFileList;
    private javax.swing.JList RemoteFileList;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
